using System.Windows.Input;

namespace Controls
{
    public class TimeTextBox : TextBoxEx
    {
        private TimeTextBox leftBox;

        private TimeTextBox rightBox;

        public TimeType Type = TimeType.Hours;

        public void setNeighbour(TimeTextBox left, TimeTextBox right)
        {
            leftBox = left;
            rightBox = right;
        }

        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            base.OnPreviewKeyDown(e);
            if (e.Key == Key.Back && base.CaretIndex == 0 && leftBox != null && base.SelectionLength == 0)
            {
                leftBox.Focus();
                leftBox.CaretIndex = leftBox.Text.Length;
                e.Handled = true;
            }
            if (e.Key == Key.Left && base.CaretIndex == 0 && leftBox != null)
            {
                leftBox.Focus();
                leftBox.CaretIndex = leftBox.Text.Length;
                e.Handled = true;
            }
            if (e.Key == Key.Right && base.CaretIndex == base.Text.Length && rightBox != null)
            {
                rightBox.Focus();
                rightBox.CaretIndex = 0;
                e.Handled = true;
            }
        }

        protected override void OnPreviewTextInput(TextCompositionEventArgs e)
        {
            base.OnPreviewTextInput(e);
            char c = char.Parse(e.Text);
            if (c == '.' && base.CaretIndex == base.Text.Length && rightBox != null)
            {
                rightBox.Focus();
                rightBox.SelectAll();
                e.Handled = true;
            }
            else if (c < '0' || c > '9')
            {
                e.Handled = true;
            }
            else if (base.Text.Length >= 2 && base.SelectionLength == 0)
            {
                e.Handled = true;
            }
        }

        protected override void OnTextInput(TextCompositionEventArgs e)
        {
            base.OnTextInput(e);
            int result;
            if (!int.TryParse(base.Text, out result) || result < 0)
            {
                return;
            }
            switch (Type)
            {
                case TimeType.Hours:
                    if (result > 23)
                    {
                        base.Text = "23";
                    }
                    if (base.Text.Length == 2 && base.CaretIndex == base.Text.Length && rightBox != null)
                    {
                        rightBox.Focus();
                        rightBox.SelectAll();
                    }
                    break;
                case TimeType.Min:
                case TimeType.Sec:
                    if (result > 59)
                    {
                        base.Text = "59";
                    }
                    if (base.Text.Length == 2 && base.CaretIndex == base.Text.Length && rightBox != null)
                    {
                        rightBox.Focus();
                        rightBox.SelectAll();
                    }
                    break;
            }
        }
    }
}
